package com.eternal.data.ui;

import android.content.Context;
import android.util.AttributeSet;

import com.eternal.base.concat.BluetoothEvent;
import com.eternal.base.concat.TmpHum;
import com.eternal.base.global.BluetoothKey;
import com.eternal.base.protocol.ProtocolTransformer;
import com.eternal.framework.utils.KLog;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Locale;

public class GraphTmpBarView extends GraphBarView {
    private boolean isDegree=true;
    private DecimalFormat format;

    public GraphTmpBarView(Context context) {
        this(context, null);
    }

    public GraphTmpBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GraphTmpBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        format=new DecimalFormat("#.0");
        format.setRoundingMode(RoundingMode.FLOOR);
    }

    public void setDegree(boolean isDegree){
        this.isDegree=isDegree;
    }

    @Override
    TimeValue getItem(int position) {
        TmpHum temp=adapter.getItem(position);
        if(isDegree){
            return new TimeValue(temp.time, temp.getTmpValue());
        }
        return new TimeValue(temp.time, (int) (ProtocolTransformer.getFah(temp.getTmpValue() / 100f) * 100));
    }

    @Override
    int getMax() {
        if(isDegree||adapter.maxTmp==0){
            return adapter.maxTmp;
        }
        return (int)(ProtocolTransformer.getFah(adapter.maxTmp / 100f) * 100);
    }

    @Override
    int getMin() {
        if(isDegree||adapter.minTmp==0){
            return adapter.minTmp;
        }else{
            return ((int) (ProtocolTransformer.getFah(adapter.minTmp / 100f) * 100));
        }
    }

    @Override
    String formatValue(int value) {
        if(isDegree){
            return format.format(value/100f)+"℃";
        }
        return format.format(ProtocolTransformer.getFah(value / 100f)) + "℉";
    }

    @Override
    String formatMaxMin(int value) {
        if(isDegree){
            if(value== BluetoothKey.EMPTY){
                return "--℃";
            }
            return format.format(value/100f)+"℃";
        }
        if(value==BluetoothKey.EMPTY){
            return "--℉";
        }
        return format.format(ProtocolTransformer.getFah(value / 100f))+"℉";
    }
}
